# -*- coding: utf-8 -*-
'''
@author: wangwl
@file: sys_code
@time: 2021/07/6 10:57
@desc:
'''

from django.db.models import Q
from components.models import SysCode
from utils.mysql_utils import query_to_list


def get_syscode_father_data(sys_code, type=""):
    sql_parameter = []
    if sys_code:
        sql_fragment = "AND b.sys_code = %s"
        sql_parameter.append(sys_code)
    else:
        sql_fragment = ''

    if type == "app":
        sql = """
        SELECT a.*
        FROM sys_code a, sys_code b
        WHERE a.sys_code_father = b.sys_code_id
        AND  b.sys_code_type = "app_deploy_type"
        AND  a.sys_code_type = "app_conf_manage_type"
        %s
        """%sql_fragment
    else:
        sql = """
        SELECT a.*
        FROM sys_code a, sys_code b
        WHERE a.sys_code_father = b.sys_code_id
        AND  b.sys_code_type = "soft_install_type"
        AND  a.sys_code_type = "soft_install_conf_type"
        %s
        """%sql_fragment

    data = query_to_list(sql, sql_parameter, False)

    return data


def get_syscode_father_child(sys_code_type):
    sql_parameter = []
    if sys_code_type:
        sql_fragment = "WHERE a.sys_code_type = %s"
        sql_parameter.append(sys_code_type)
    else:
        sql_fragment = ''

    sql = """
    SELECT b.sys_code_name AS conf_manage_name,b.sys_code AS conf_manage_nick 
    FROM sys_code a
    LEFT JOIN sys_code b ON	b.sys_code_father = a.sys_code_id
    %s
    """ %sql_fragment

    data = query_to_list(sql, sql_parameter, False)

    return data